# Maintainer: Naveen M K <naveen521kk@gmail.com>

_pyname=flit
_realname=flit
pkgbase=mingw-w64-python-${_realname}
pkgname=(
    "${MINGW_PACKAGE_PREFIX}-python-${_realname}"
    "${MINGW_PACKAGE_PREFIX}-python-${_realname}-core"
)
pkgver=3.2.0
pkgrel=1
pkgdesc='Simplified packaging of Python modules (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/takluyver/flit"
license=('BSD')
depends=(
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-toml"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-pip"
)
source=("$pkgbase-$pkgver.tar.gz::$url/archive/$pkgver.tar.gz")
sha512sums=('b40768fb4b0b2a2e3116ac790dfa629da5a57cda34b10abb8ce6a8febcc3436795d5122dcfa3db2c6d07d493f00391d2ca7300112561c8ff2a0b04d77c879662')

prepare() {
  cd "$srcdir"
  rm -rf python-build-${CARCH} | true
  cp -r "${_pyname//_/-}-$pkgver" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}/flit_core"
  ${MINGW_PREFIX}/bin/python -m build . -x -n -w

  cd "${srcdir}/python-build-${CARCH}"
  PYTHONPATH=flit_core  ${MINGW_PREFIX}/bin/python -m build . -x -n -w  
  cp flit_core/dist/*.whl dist/
}

package_python-flit() {
  depends+=(
    "${MINGW_PACKAGE_PREFIX}-python-flit-core"
    "${MINGW_PACKAGE_PREFIX}-python-requests" 
    "${MINGW_PACKAGE_PREFIX}-python-docutils"
  ) 

  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python -m pip install dist/flit-${pkgver}-py3-none-any.whl \
    --compile \
    --no-deps \
    --no-warn-script-location \
    --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}


package_python-flit-core() {
  pkgdesc+='Simplified packaging of Python modules (core backend) (mingw-w64)'
  
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python -m pip install dist/flit_core-${pkgver}-py3-none-any.whl \
    --compile \
    --no-deps \
    --no-warn-script-location \
    --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}-core/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
